package com.ruoyi.web.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.EasyNum;
import com.ruoyi.system.domain.Ideas;
import com.ruoyi.system.domain.TotalHistory;
import com.ruoyi.system.domain.UserInfoForSaveIdeas;
import com.ruoyi.system.domain.vo.EasyNumResult;
import com.ruoyi.system.mapper.EasyNumMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.*;


@RestController
@RequestMapping("/number")
public class AddController extends BaseController {
    @Autowired
    private EasyNumMapper easyNumMapper;

    @GetMapping("/create")
    private void create(){
        for (int i = 0; i <= 5; i++){
            for (int j = 0 ; j <=5; j++){
                if (i - j >=0) {
                EasyNum easyNum = new EasyNum();
                easyNum.setTitle(i +"-"+j);
                easyNum.setResult(String.valueOf(i-j));
                easyNum.setType("五以内减法");

                    easyNumMapper.CreateNum(easyNum);
                }
            }
        }
    }

    //saveIdeas
    @PostMapping("/saveIdeas")
    @RepeatSubmit(interval = 600000,message = "网络异常，请稍后重试")
    private void saveIdeas(@RequestBody Ideas ideas, HttpServletRequest request){
        String token = request.getHeader("token");
        if (StringUtils.isNotEmpty(token)) {
        // find nickName
            UserInfoForSaveIdeas info = easyNumMapper.findNickName(token);
            ideas.setNickName(info.getNickName());
            ideas.setOpenId(info.getUserName());
            ideas.setData(DateUtil.today());
            ideas.setStatus("未开始");
                easyNumMapper.saveIdeas(ideas);
        }
        System.out.println(ideas);
    }
    @GetMapping("/getIdeas")
    private TableDataInfo getIdeas(Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Ideas> ideasList = easyNumMapper.findIdeasList();
        return getDataTable(ideasList);
    }

    @GetMapping("/historyTotal")
    private AjaxResult totalhistory ( HttpServletRequest request){
        String token = request.getHeader("token");
        if (StringUtils.isNotEmpty(token)){
            Integer user = easyNumMapper.findUserByToken(token);
            if (user != 0){
                TotalHistory totalHistory = easyNumMapper.findHistory(token);
                return AjaxResult.success(totalHistory);
            }
        }
        return null;
    }
    @GetMapping("/historyList")
    private TableDataInfo historyList(@Param("pageNum") Integer pageNum,@Param("pageSize") Integer pageSize, HttpServletRequest request){
        String token = request.getHeader("token");
        if (StringUtils.isNotEmpty(token)) {
            Integer user = easyNumMapper.findUserByToken(token);
            if (user != 0) {
                PageHelper.startPage(pageNum, pageSize);
                List<EasyNumResult> resultList = easyNumMapper.finrResultList(token);
                return getDataTable(resultList);
            }
        }
        return null;
    }


//save log
    @PostMapping("/saveEasyResult")
    private void EasyResult(@RequestBody EasyNumResult easyNumResult, HttpServletRequest request){
        String token = request.getHeader("token");
        if (StringUtils.isNotEmpty(token)) {
            easyNumResult.setUser(token);
            easyNumResult.setTime(DateUtil.parse(DateUtil.now()));
            try {
                easyNumMapper.insertLog(easyNumResult.getUser(), easyNumResult.getTitle(), easyNumResult.getResult(), easyNumResult.getTime(), easyNumResult.getRemark(),easyNumResult.getStatu());
            }catch (Exception e){

            }
        }
    }


    //加法
    @GetMapping("/easyNumOne")
    private AjaxResult adda(Integer num,Integer max,Integer min){
        Set<EasyNum> easyNumList = new HashSet<>();
        Set<EasyNum> easyNumSet = addaone(num,max,easyNumList,min);
        return  AjaxResult.success(easyNumSet);
    }
    private Set<EasyNum> addaone(Integer num,Integer max,Set<EasyNum> easyNumList,Integer min){
        for (int n = 0 ; n < num ; n++) {
            int a = RandomUtil.randomInt(min, max);
            int b = RandomUtil.randomInt(min, max);
            String title = a + "+" + b + "=";
            Integer result = a + b;
            EasyNum easyNum = new EasyNum();
            easyNum.setTitle(title);
            easyNum.setResult(String.valueOf(result));
            easyNumList.add(easyNum);
        }
        if (easyNumList.size() < num){
            addaone(num - easyNumList.size(),max,easyNumList,min);
        }
        return easyNumList;
    }

    //减法
    @GetMapping("/easyNumTwo")
    private AjaxResult addb(Integer num,Integer max,Integer min){
        Set<EasyNum> easyNumList = new HashSet<>();
        Set<EasyNum> easyNumSet = addbone(num,max,easyNumList,min);
        return  AjaxResult.success(easyNumSet);
    }
    private Set<EasyNum> addbone(Integer num,Integer max,Set<EasyNum> easyNumList,Integer min){
        for (int n = 0 ; n < num ; n++) {
            int a = RandomUtil.randomInt(min, max);
            int b = RandomUtil.randomInt(min, max);
            String title = a + "-" + b + "=";
            Integer result = a - b;
            if (result <0){
                continue;
            }
            EasyNum easyNum = new EasyNum();
            easyNum.setTitle(title);
            easyNum.setResult(String.valueOf(result));
            easyNumList.add(easyNum);
        }
        if (easyNumList.size() < num){
            addbone(num - easyNumList.size(),max,easyNumList,min);
        }
        return easyNumList;
    }

    //加减法
    @GetMapping("/easyNumThree")
    private AjaxResult addc(Integer num,Integer max,Integer min) {
        Set<EasyNum> easyNumList = new HashSet<>();
        Set<EasyNum> easyNumSet = addc(num, max, easyNumList,min);
        return  AjaxResult.success(easyNumSet);
    }
    private Set<EasyNum> addc(Integer num,Integer max,Set<EasyNum> easyNumList,Integer min){
        for (int n = 0 ; n < num ; n++) {
            String title = "";
            Integer result = 0;
            int a = RandomUtil.randomInt(min, max);
            int b = RandomUtil.randomInt(min, max);
            int c = RandomUtil.randomInt(min,100);//0 - , 1 +
            if (c%2 == 0 && a >=b){
                title = a + "-" + b;
                result = a - b;
                EasyNum easyNum = new EasyNum();
                easyNum.setTitle(title);
                easyNum.setResult(String.valueOf(result));
                easyNumList.add(easyNum);
            }else if (c%2 ==0 && a <= b){
                continue;
            }else {
                title = a + "+" + b;
                result = a + b;
                EasyNum easyNum = new EasyNum();
                easyNum.setTitle(title);
                easyNum.setResult(String.valueOf(result));
                easyNumList.add(easyNum);
            }
        }
        if (easyNumList.size()<num){
            addc(num, max, easyNumList,min);
        }

        return  easyNumList;
    }

    //乘法
    @GetMapping("/ride")
    private AjaxResult RideOne(Integer num, Integer max,Integer min){
        Set<EasyNum> easyNumList = new HashSet<>();
        Set<EasyNum> easyNumSet = RideTwo(num,max,min,easyNumList);
        return  AjaxResult.success(easyNumSet);
    }
    private Set<EasyNum> RideTwo(Integer num, Integer max,Integer min,Set<EasyNum> easyNumList){
        for (int x = 0 ; x < num; x++) {
            EasyNum easyNum = new EasyNum();
            Integer a = RandomUtil.randomInt(min, max);
            Integer b = RandomUtil.randomInt(min, max);
            easyNum.setTitle(a +"✖"+b+"=");
            easyNum.setResult(String.valueOf(a * b));
            easyNumList.add(easyNum);
        }
        return easyNumList;
    }

    //除法
    @GetMapping("/except")
    private AjaxResult ExceptOne(Integer num, Integer max,Integer min){
        Set<EasyNum> easyNumList = new HashSet<>();
        for (int x = 0 ; x < num; x++) {
            EasyNum easyNum = new EasyNum();
            Integer a = RandomUtil.randomInt(min, max);
            Integer b = RandomUtil.randomInt(1, max);
            if (a >=b && a %b ==0) {
                easyNum.setTitle(a + "➗" + b + "=");
                easyNum.setResult(String.valueOf(a / b));
                easyNumList.add(easyNum);
            }else {
                x--;
            }
        }
        return  AjaxResult.success(easyNumList);
    }

    //乘除法
    @GetMapping("/rideAndExcept")
    private AjaxResult rideAndExcept(Integer num, Integer max){
        if (num > 500){
            return AjaxResult.error("no message");
        }
        Set<EasyNum> easyNumList = new HashSet<>();
        for (int x = 0 ; x < num; x++) {
            EasyNum easyNum = new EasyNum();
            Integer a = RandomUtil.randomInt(0,max);
            Integer b = RandomUtil.randomInt(0,max);// 0 ->/   1 -> *
            if (b%2 ==0){
                Integer c = RandomUtil.randomInt(1,max);
                if (a %c ==0 && a >=c){
                    easyNum.setTitle(a + "➗" + c + "=");
                    easyNum.setResult(String.valueOf(a / c));
                    easyNumList.add(easyNum);
                       if (easyNumList.size() < num){
                x--;
            }
                }else {
                    x--;
                }
            }else {
                Integer c = RandomUtil.randomInt(0,max);
                easyNum.setTitle(a +"✖"+c+"=");
                easyNum.setResult(String.valueOf(a * c));
                easyNumList.add(easyNum);
                   if (easyNumList.size() < num){
                x--;
            }
            }
        }
        return AjaxResult.success(easyNumList);
    }

    //比大小
    @GetMapping("/bdx")
    private AjaxResult bdx(Integer num,Integer max){
        Set<EasyNum> easyNumList = new HashSet<>();
        for (int i = 0; i < num; i++) {
            EasyNum easyNum = new EasyNum();
            Integer maxNum = RandomUtil.randomInt(0,max);
            Integer min = RandomUtil.randomInt(0,max);
            easyNum.setTitle(maxNum +"\uD83D\uDD32"+min );
//            1 >   2 <  3 =
            if (maxNum > min){
                easyNum.setResult(">");
            }else if (maxNum < min){
                easyNum.setResult("<");
            }else {
                easyNum.setResult("=");
            }
            easyNumList.add(easyNum);
               if (easyNumList.size() < num){
                i--;
            }
        }
        return AjaxResult.success(easyNumList);
    }
    
    //找规律
    @GetMapping("/zgl")
    private AjaxResult zgl(Integer num){
        if (num >20){
            return AjaxResult.warn("no message");
        }
        Set<EasyNum> easyNumList = new HashSet<>();
        for (int i = 0; i < num; i++) {
            EasyNum easyNum = new EasyNum();
            Integer first = RandomUtil.randomInt(0,7);
            Integer second = RandomUtil.randomInt(1,9);
            easyNum.setTitle(first +"\uD83D\uDD32"+(first+second)+"\uD83D\uDD32"+(first+second+second)+"\uD83D\uDD32"+(first+second+second+second));
            easyNum.setResult(String.valueOf(second));
            easyNumList.add(easyNum);
        }
        return AjaxResult.success(easyNumList);
    }

    //加减比大小
    @GetMapping("/addbdx")
    private AjaxResult addbdx(Integer num, Integer max) {
        Set<EasyNum> easyNumList = new HashSet<>();
        Set<EasyNum> easyNumSet = createaddbdx(easyNumList, num, max);
        return AjaxResult.success(easyNumSet);
    }
    private Set<EasyNum> createaddbdx(Set<EasyNum> easyNumList,Integer num, Integer max){
        for (int i = 0; i < num; i++) {
            EasyNum easyNum = new EasyNum();
            Integer a = RandomUtil.randomInt(0, max);
            Integer b = RandomUtil.randomInt(0, max);
            Integer c = RandomUtil.randomInt(0, max);
            Integer flagA = RandomUtil.randomInt(0, 100);// 0 -   ,1  +
            if (flagA % 2 == 0) {
                if (a - b > c) {
                    easyNum.setTitle(a + "-" + b + "\uD83D\uDD32" + c);
                    easyNum.setResult(">");
                } else if (a - b == c) {
                    easyNum.setTitle(a + "-" + b + "\uD83D\uDD32" + c);
                    easyNum.setResult("=");
                } else {
                   continue;
                }
            } else {
                if (a + b > c) {
                    easyNum.setTitle(a + "+" + b + "\uD83D\uDD32" + c);
                    easyNum.setResult(">");
                } else if (a + b == c) {
                    easyNum.setTitle(a + "+" + b + "\uD83D\uDD32" + c);
                    easyNum.setResult("=");
                } else {
                    easyNum.setTitle(a + "+" + b + "\uD83D\uDD32" + c);
                    easyNum.setResult("<");
                }
            }
            easyNumList.add(easyNum);
        }
        if (easyNumList.size()<num){
            createaddbdx(easyNumList,num,max);
        }
        return easyNumList;
    }

    //连加
    @GetMapping("/lianjia")
    private AjaxResult lianjia(Integer num, Integer max, String flag){
        Set<EasyNum> easyNumList = new HashSet<>();
        for (int i = 0; i < num; i++) {
            EasyNum easyNum = new EasyNum();
            Integer a = RandomUtil.randomInt(0,max);
            Integer b = RandomUtil.randomInt(0,max);
            Integer c = RandomUtil.randomInt(0,max);
            if ("1".equals(flag)){// 0 -  , 1+
                easyNum.setTitle(a +"+"+b +"+"+c);
                easyNum.setResult(String.valueOf(a+b+c));
            }else if ("0".equals(flag) ){
                if ((a >b) && ((a-b) >c)) {
                    easyNum.setTitle(a + "-" + b + "-" + c);
                    easyNum.setResult(String.valueOf(a - b - c));
                }else {
                    i--;
                    continue;
                }
            }
            easyNumList.add(easyNum);
        }
        return AjaxResult.success(easyNumList);
    }

    //十位数加减个位数
    @GetMapping("/swjjj")
    private AjaxResult swsjj(Integer num, Integer max, String flag){
        Set<EasyNum> easyNumList = new HashSet<>();
        for (int i = 0; i < num; i++) {
            EasyNum easyNum = new EasyNum();
            Integer a = RandomUtil.randomInt(10,max);
            Integer b = RandomUtil.randomInt(0,9);
            if ("1".equals(flag)){ // 0  -,  1 +
                easyNum.setTitle(a +"+"+ b);
                easyNum.setResult(String.valueOf(a+b));
            }else {
                easyNum.setTitle(a +"-"+ b);
                easyNum.setResult(String.valueOf(a-b));
            }
            easyNumList.add(easyNum);
        }
        return AjaxResult.success(easyNumList);
    }

    //加法换算
    @GetMapping("/hs")
    private AjaxResult hs(Integer num, Integer max, String flag){
        Set<EasyNum> easyNumList = new HashSet<>();
        for (int i = 0; i < num; i++) {
            EasyNum easyNum = new EasyNum();
            Integer a = RandomUtil.randomInt(0,max);
            Integer b = RandomUtil.randomInt(0,max);
            if (Objects.equals(flag, "1")){// 0 - ,1+
                easyNum.setTitle( a +"+"+"\uD83D\uDD32"+"="+(a+b));
                easyNum.setResult(String.valueOf(b));
            }else {
                if (a >b){
                    easyNum.setTitle( a +"-"+"\uD83D\uDD32"+"="+(a-b));
                    easyNum.setResult(String.valueOf(b));
                }else {
                    i--;
                }
            }
            easyNumList.add(easyNum);
            if (easyNumList.size()<num){
                i--;
            }
        }

        return AjaxResult.success(easyNumList);
    }

    //带括号运算
    @GetMapping("/dkhys")
    private AjaxResult dkhys(Integer num, Integer max){
        Set<EasyNum> easyNumList = new HashSet<>();
        for (int i = 0; i < num; i++) {
            EasyNum easyNum = new EasyNum();
            Integer a = RandomUtil.randomInt(0,max);
            Integer b = RandomUtil.randomInt(0,max);
            Integer c = RandomUtil.randomInt(0,max);
            Integer fal = RandomUtil.randomInt(0,100); // 0 -  1 +
            if (fal%2==0){ //0- ,1+
                if (a > b+c && a >b && b >c){
                    easyNum.setTitle(a+"-"+"("+b+"-"+c+")"+"=");
                    easyNum.setResult(String.valueOf(a-(b-c)));
                    easyNumList.add(easyNum);
                }else {
                    i--;
                }
            }else {
                if (a > b+c ){
                    easyNum.setTitle(a+"-"+"("+b+"+"+c+")"+"=");
                    easyNum.setResult(String.valueOf(a-(b+c)));
                    easyNumList.add(easyNum);
                }else {
                    i--;
                }
            }
        }
        return AjaxResult.success(easyNumList);
    }

    //
    @GetMapping("/lwsljj")
    private AjaxResult lwsljj(Integer num, Integer max,Integer min){
        Set<EasyNum> easyNumList = new HashSet<>();
        Set<EasyNum> easyNumSet = lesljj(num, max, min, easyNumList);
        return AjaxResult.success(easyNumSet);
    }

    private Set<EasyNum> lesljj(Integer num, Integer max,Integer min,Set<EasyNum> easyNumList){
        for (Integer i = 0 ; i < num; i++){
            Integer a = RandomUtil.randomInt(min,max);
            Integer b = RandomUtil.randomInt(min,max);
            Integer c = RandomUtil.randomInt(min,max);
            Integer d = RandomUtil.randomInt(min,max);// 0 -  1 +
            Integer e = RandomUtil.randomInt(min,max);// 0 -  1 +
            EasyNum easyNum = new EasyNum();
            if ((d%2 ==0) && (e%2 == 0)){
                easyNum.setTitle(a+"+"+b+"+"+c);
                easyNum.setResult(String.valueOf(a+b+c));
                easyNumList.add(easyNum);
            }else if ((d%2==0)&&(e%2==1)&&(b>=c)){
                easyNum.setTitle(a+"+"+b+"-"+c);
                easyNum.setResult(String.valueOf(a+b-c));
                easyNumList.add(easyNum);
            }else if ((d%2==1)&&(e%2==1)&&(a-b>=c)){
                easyNum.setTitle(a+"-"+b+"-"+c);
                easyNum.setResult(String.valueOf(a-b-c));
                easyNumList.add(easyNum);
            }else if ((d%2==1)&&(e%2==0)&&(a>=b)){
                easyNum.setTitle(a+"-"+b+"+"+c);
                easyNum.setResult(String.valueOf(a-b+c));
                easyNumList.add(easyNum);
            }else {
                i--;
            }
        }
        return easyNumList;
    }

    //
    @GetMapping("/lwsljjbdx")
    private AjaxResult lwsljjbdx(){
        Set<EasyNum> easyNumList = new HashSet<>();
        Set<EasyNum> easyNumSet = lesljj(1, 33, 0, easyNumList);
        Integer b = RandomUtil.randomInt(0,100);
        for (EasyNum easy : easyNumSet){
            if (Convert.toInt(easy.getResult()) > b){
                easy.setTitle(easy.getTitle()+"\uD83D\uDD32"+b);
                easy.setResult(">");
            }else if(Convert.toInt(easy.getResult()) < b){
                easy.setTitle(easy.getTitle()+"\uD83D\uDD32"+b);
                easy.setResult("<");
            }else {
                easy.setTitle(easy.getTitle()+"\uD83D\uDD32"+b);
                easy.setResult("=");
            }
        }
        return AjaxResult.success(easyNumSet);
    }

    //
    @GetMapping ("/biaoneichengjia")
    private AjaxResult bncf(Integer num, Integer max,Integer min){
        Set<EasyNum> easyNumList = new HashSet<>();
        Set<EasyNum> easyNumSet = RideTwo(num, max, min, easyNumList);
        Integer i = RandomUtil.randomInt(0,max);
        for (EasyNum easy : easyNumSet){
            String title = easy.getTitle().substring(0,easy.getTitle().indexOf("="));
            easy.setTitle(title +"+"+i+"=");
            easy.setResult(String.valueOf(Convert.toInt(easy.getResult())+i));
        }
        return AjaxResult.success(easyNumSet);
    }

    @GetMapping("/biaoneichengjian")
    private AjaxResult bncjj(){
        Set<EasyNum> easyNumList = new HashSet<>();
        Set<EasyNum> easyNum = bncjjone(easyNumList);
        return AjaxResult.success(easyNum);
    }
    private Set<EasyNum> bncjjone(Set<EasyNum> easyNumList){
        EasyNum easyNum = new EasyNum();
        Integer a = RandomUtil.randomInt(0,9);
        Integer b = RandomUtil.randomInt(0,9);
        Integer c = RandomUtil.randomInt(0,9);
        if (a *b <c){
            easyNumList = bncjjone(easyNumList);
        }else {
            easyNum.setTitle(a +"X" +b +"-"+c+"=");
            easyNum.setResult(String.valueOf((a*b)-c));
            easyNumList.add(easyNum);
        }
        return easyNumList;
    }

    //shang
    @GetMapping("/shang")
    private AjaxResult ShangOne(Integer num, Integer max,Integer min){
        Set<EasyNum> easyNumList = new HashSet<>();
        for (int x = 0 ; x < num; x++) {
            EasyNum easyNum = new EasyNum();
            Integer a = RandomUtil.randomInt(min, max);
            Integer b = RandomUtil.randomInt(1, 9);
            if (a >=b ) {
                easyNum.setTitle(a + "➗" + b + "=");
                easyNum.setResult(String.valueOf(a / b));
                easyNumList.add(easyNum);
            }else {
                x--;
            }
        }
        return  AjaxResult.success(easyNumList);
    }

    //yu shu
    @GetMapping("/yushu")
    private AjaxResult yushu(){
        Set<EasyNum> easyNumList = new HashSet<>();
        EasyNum easyNum = new EasyNum();
        int i = RandomUtil.randomInt(1, 99);
        int o = RandomUtil.randomInt(1, 99);
        int yu = i % o;
        easyNum.setTitle(i+"➗"+o +"=");
        easyNum.setResult(String.valueOf(yu));
        easyNumList.add(easyNum);
        return AjaxResult.success(easyNumList);
    }

    //fen shu bdx
    @GetMapping("/fsbdx")
    private AjaxResult fsbdx(){
        Set<EasyNum> easyNumList = new HashSet<>();
        EasyNum easyNum = new EasyNum();
        double a = Double.parseDouble(NumberUtil.roundStr(RandomUtil.randomDouble(0, 10),2));
        double b = Double.parseDouble(NumberUtil.roundStr(RandomUtil.randomDouble(0, 10),2));
        if (a >b){
            easyNum.setResult(">");
        }else if (a ==b){
            easyNum.setResult("=");
        }else {
            easyNum.setResult("<");
        }
        easyNum.setTitle(a +"\uD83D\uDD32"+b);
        easyNumList.add(easyNum);
        return AjaxResult.success(easyNumList);
    }

    @GetMapping("/fenshujs")
    private AjaxResult fenshujs(){
        Set<EasyNum> easyNumList = new HashSet<>();
        EasyNum easyNum = new EasyNum();
        double a = Double.parseDouble(NumberUtil.roundStr(RandomUtil.randomDouble(0, 10),2));
        double b = Double.parseDouble(NumberUtil.roundStr(RandomUtil.randomDouble(0, 10),2));
        int i = RandomUtil.randomInt(0, 99);// 0 -   1  +
        if (a >= b ){
            if (i%2 ==0){
                easyNum.setTitle(a +"-"+b+"=");
                easyNum.setResult(String.valueOf(a-b));
            }else {
                easyNum.setTitle(a +"+"+b+"=");
                easyNum.setResult(String.valueOf(a+b));
            }
        }else {
            easyNum.setTitle(a +"+"+b+"=");
            easyNum.setResult(String.valueOf(a+b));
        }
        easyNumList.add(easyNum);
        return AjaxResult.success(easyNumList);
    }

}
